import {
  Column,
  Entity,
  Index,
  JoinColumn,
  JoinTable,
  ManyToMany,
  ManyToOne,
  OneToMany,
} from 'typeorm';
import { CommonEntity, CommonStatus } from '~/common/entity/common.entity';
import { ArticleCategoryEntity } from '../article-category/article-category.entity';
import { ArticleContentCollectEntity } from './article-content-collect/aritcle-content-collect.entity';
import { TenantEntity } from '~/modules/tenant/tenant.entity';
import { ArticleContentLikeEntity } from './article-content-like/article-content-like.entity';
import { Exclude } from 'class-transformer';
import { CategoryEntity } from '~/modules/category/category.entity';
import { ArticleContentUserReviewEntity } from './article-content-user-review/article-content-user-review.entity';
import { ArticleContentShareEntity } from './article-content-share/article-content-share.entity';

// 文章类型枚举
export enum ArticleContentType {
  SCIENCE_KNOWLEDGE = 'SCIENCE_KNOWLEDGE', // 科普知识 - 推荐知识
  MEDICAL_KNOWLEDGE = 'MEDICAL_KNOWLEDGE', // 医学知识 - 任务知识
}

@Entity({ name: 'ah_article_content' })
@Index(['id'])
export class ArticleContentEntity extends CommonEntity {
  @Column({ type: 'varchar', length: 150, comment: '编码', nullable: true, name: 'code' })
  code: string;

  // 名称、作者、浏览量、富文本
  @Column({ type: 'varchar', length: 50, comment: '名称' })
  name: string;

  @Column({ type: 'varchar', length: 200, comment: '横幅', nullable: true })
  banner: string;

  @Column({ type: 'varchar', length: 50, comment: '作者' })
  author: string;

  @Column({
    type: 'enum',
    enum: ArticleContentType,
    comment: '文章类型',
    default: ArticleContentType.MEDICAL_KNOWLEDGE,
  })
  type: ArticleContentType;

  @Column({ type: 'int', comment: '浏览量', default: 0, name: 'view_count' })
  viewCount: number;

  // 点赞量
  @Column({ type: 'int', comment: '点赞量', default: 0, name: 'like_count' })
  likeCount: number;

  // 收藏量
  @Column({ type: 'int', comment: '收藏量', default: 0, name: 'collect_count' })
  collectCount: number;

  // 分享量
  @Column({ type: 'int', comment: '分享量', default: 0, name: 'share_count' })
  shareCount: number;

  @Column({ type: 'longtext', comment: '富文本内容', nullable: true })
  content: string;

  @Column({ type: 'longtext', comment: 'html内容', nullable: true, name: 'raw_html' })
  rawHtml: string;

  @Column({
    type: 'longtext',
    comment: '视频链接（多链接逗号分隔）',
    nullable: true,
    name: 'video',
  })
  video: string;

  @Column({
    type: 'varchar',
    length: 50,
    comment: '发布时间',
    nullable: true,
    name: 'publish_time',
  })
  publishTime: string;

  @Column({ type: 'varchar', length: 150, comment: '文件名称', nullable: true, name: 'file_name' })
  fileName: string;

  @Column({ type: 'tinyint', width: 1, default: CommonStatus.YES, comment: '是否启用' })
  status: CommonStatus;

  @Exclude()
  @ManyToOne(() => TenantEntity, (tenant) => tenant.articleContents, { nullable: true })
  @JoinColumn({ name: 'tenant_id' })
  tenant: TenantEntity;

  // @ManyToMany(() => ArticleCategoryEntity, (articleCategory) => articleCategory.contents)
  // categories: ArticleCategoryEntity[];

  @Exclude()
  @ManyToMany(() => ArticleContentCollectEntity, (contentCollect) => contentCollect.content)
  contentCollects: ArticleContentCollectEntity[];

  @Exclude()
  @ManyToMany(() => ArticleContentLikeEntity, (contentCollect) => contentCollect.content)
  contentLikes: ArticleContentLikeEntity[];

	@Exclude()
	@ManyToMany(() => ArticleContentShareEntity, (contentShare) => contentShare.content)
	contentShares: ArticleContentShareEntity[];

  @ManyToOne(() => CategoryEntity, (category) => category.contents)
  @JoinColumn({ name: 'category_id' })
  category: CategoryEntity;

  @OneToMany(() => ArticleContentUserReviewEntity, (article) => article.article)
  articleContentUserReview: ArticleContentUserReviewEntity[];
}
